<?php

Class Pr_node extends Core {
    
    
    function __construct() {
        
        parent::__construct_admin();
        
        if (!$this->usr->check_rights('admin')) {
            
            show_error('Нет доступа!!!');
        }
        
        $this->load->model('node_admin');
        $this->load->model('nodelib');
        $this->load->language('node');
        $this->load->helper('form');
    }
    
    
    public function index() {
        
        $vars['list'] = $this->node_admin->get_nodes();
        
        parent::render($vars, 'admin/index', true);
    }
    
    
    public function fields($node) {
        
        $ORM = new NodeORM($node);
        
        $vars['fields'] = $ORM->get_meta();
        $vars['node'] = $node;
        
        parent::render($vars, 'admin/fields', true);
    }
    
    
    public function field_add($node = '') {
        
        if ( $this->input->post('add', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('field_name',  'Имя',       'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('field_title', 'Название',  'trim|required|min_length[2]|xss_clean');
            $this->form_validation->set_rules('field_type',  'Тип',       'trim|required|xss_clean');
            
            if ( $this->form_validation->run() ) {
                
                $vars['notification'] = lang('node_field_add_success');
                
                $field = new NodeFieldsAdmin($node);
                $field->name = set_value('field_name');
                $field->title = set_value('field_title');
                $field->type = set_value('field_type');
                $field->add();
            }
            else {
                
                $vars['notification'] = $this->form_validation->error_string();
            }
        }
        
        $fields = $this->node_admin->get_types();
        
        foreach($fields as $row) {
        
            $vars['types'][$row] = lang('field_'.$row); 
        }
        
        $vars['node'] = $node;
        
        parent::render($vars, 'admin/fields_add', true);
    }
    
    
    public function data($node) {
        
        $this->load->library('pagination');
        
        $ORM = new NodeORM($node);
        $count = $ORM->count(new NodeFilter());
        $limit = $this->config->get('pagination', 'core');
        $Filter = new NodeFilter();
        
        $data['pagination'] = $this->pagination->create($this->uri->modul_uri_string(), $count, $limit, 6);
        
        $Filter->order($node.'_id')->limit($limit, $this->pagination->offset());
        
        $data['list'] = $ORM->get($Filter)->result_array();
        $data['meta'] = $ORM->get_meta();
        $data['node'] = $node.'_';
        $data['node_free'] = $node;
        
        parent::render($data, 'admin/data_index', true);
    }
    
    
    public function data_add($node) {
        
        $ORM    = new NodeORM($node);
        $Meta   = new NodeMeta($ORM->get_meta());
        $Fields = new NodeFields($Meta, $node);
        $Valide = new NodeValidate($Meta, $node);
        
        if ( $this->input->post($node.'_add', TRUE) ) {
            
            $this->load->library('form_validation');
            $this->form_validation->set_rules($Valide->rules());
            
            if ( $this->form_validation->run() ) {
                
                $ORM->add($Valide->set($this->form_validation));
                $data['notification'] = 'Данные сохранены';
            }
            else {
                
                $data['notification'] = $this->form_validation->error_string();
            }
        }
        
        $data['fields'] = $Fields->get();
        $data['node']   = $node;
        
        parent::render($data, 'admin/data_add', true);
    }
    
    
    public function data_edit($node, $id) {
        
        $ORM    = new NodeORM($node);
        $Meta   = new NodeMeta($ORM->get_meta());
        $Fields = new NodeFields($Meta, $node);
        $Filter = new NodeFilter();
        $Valide = new NodeValidate($Meta, $node);
        
        $Filter->where($node.'_id', $id)->limit(1);
        
        if ( $this->input->post($node.'_add', TRUE) ) {
            
            $this->load->library('form_validation');
            $this->form_validation->set_rules($Valide->rules());
            
            if ( $this->form_validation->run() ) {
                
                $ORM->update($Filter, $Valide->set($this->form_validation));
                $data['notification'] = 'Данные сохранены';
            }
            else {
                
                $data['notification'] = $this->form_validation->error_string();
            }
        }
        
        $data['fields'] = $Fields->get($ORM->get($Filter)->row_array());
        $data['node']   = $node;
        
        parent::render($data, 'admin/data_add', true);
    }
    
    
    public function data_del($node, $id) {
        
        $ORM    = new NodeORM($node);
        $Filter = new NodeFilter();
        
        $ORM->delete($Filter->where($node.'_id', $id));
        
        modul_redirect('modul/node/data/'.$node);
    }
}